package UnitTest;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: dingtian
 * @Date: 2020-2-16 14 40
 * @Description: leetcode 39
 */
public class CombinationSum1 {

    public static void main(String[] args) {
        int[] candidates = {2, 3, 6, 7};
        int target = 7;
        CombinationSum1 solution = new CombinationSum1();
        List<List<Integer>> list = solution.combinationSum(candidates, target);
        System.out.println(list);
    }

    private  List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> res = new ArrayList<>();
        helper(candidates,new ArrayList<>(),res,target,0);
        return res;
    }

    private void helper(int[] candiadates,List<Integer> targetList, List<List<Integer>> res, int target,int index) {
        if (target <= 0) {
            if (target == 0) {
                res.add(new ArrayList<>(targetList));
            }
            return;
        }
        for (int i = index;i<candiadates.length;i++){
            targetList.add(candiadates[i]);
            helper(candiadates, targetList, res, target-candiadates[i], i);
            targetList.remove(targetList.size()-1);
        }
    }
}
